﻿using System;
using System.ComponentModel;
using System.Windows;

namespace WaitLess.App.ViewModels
{
    public class ViewModelBase : INotifyPropertyChanged
    {
        private bool _isDataLoaded;
        public bool IsDataLoaded 
        {
            get { return _isDataLoaded; }
            protected set { SetProperty(ref _isDataLoaded, value, "IsDataLoaded"); }
        }

        public virtual void LoadData()
        {
            IsDataLoaded = true;
        }

        public void UpdateUI(Action action)
        {
            Deployment.Current.Dispatcher.BeginInvoke(action);  
        }

        public void UpdateUI(Delegate d, params object[] args)
        {
            Deployment.Current.Dispatcher.BeginInvoke(d, args);
        }

        protected bool SetProperty<T>(ref T storage, T value, string propertyName)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            var eventHandler = this.PropertyChanged;
            if (eventHandler != null)
            {
                eventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
